package com.itheima.web.service.impl.sms;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.service.sms.IFlashPromotionProductRelationService;
import com.itheima.service.sms.IFlashPromotionSessionService;
import com.itheima.vo.sms.FlashPromotionProductRelation;
import com.itheima.vo.sms.FlashPromotionSession;
import com.itheima.vo.sms.FlashPromotionSessionVo;
import com.itheima.web.mapper.sms.FlashPromotionSessionMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 限时购场次表 服务实现类
 */
@DubboService
public class FlashPromotionSessionServiceImpl extends ServiceImpl<FlashPromotionSessionMapper, FlashPromotionSession> implements IFlashPromotionSessionService {
    @Autowired
    private IFlashPromotionProductRelationService flashPromotionProductRelationService;
    @Autowired
    private IFlashPromotionSessionService flashPromotionSessionService;

    /**
     * 获取全部可选场次及其数量
     * @param flashPromotionId
     * @return
     */
    @Override
    public List<FlashPromotionSessionVo> selectList(Long flashPromotionId) {
        //先根据前端传回的数据进行筛选
        QueryWrapper<FlashPromotionProductRelation> lqw=new QueryWrapper<>();
        lqw.eq(flashPromotionId!=null,"flash_promotion_id",flashPromotionId);
        //查询活动产品关系表，查询符合对应的场次
        lqw.groupBy("flash_promotion_session_id");
        lqw.select("flash_promotion_session_id","count(*) as productCount");
        List<Map<String, Object>> maps = flashPromotionProductRelationService.listMaps(lqw);
        List<FlashPromotionSessionVo> list=maps.stream().map(
                item->{
                    FlashPromotionSessionVo flashPromotionSessionVo=new FlashPromotionSessionVo();
                    Long flash_promotion_session_id = (Long) item.get("flash_promotion_session_id");
                    FlashPromotionSession byId = flashPromotionSessionService.getById(flash_promotion_session_id );
                    BeanUtils.copyProperties(byId,flashPromotionSessionVo);
                    Long productCount = (Long) item.get("productCount");
                    flashPromotionSessionVo.setProductCount(productCount);
                    return flashPromotionSessionVo;
                }
        ).collect(Collectors.toList());


        return list;
        //分组后已经去除重复了，并且每个时间段商品多少都有了
        //set存储对应的时间段信息，treeset去除重复
     /*   Set<Long> set=new TreeSet<>();
        for (FlashPromotionProductRelation flashPromotionProductRelation : list) {
            set.add(flashPromotionProductRelation.getFlashPromotionSessionId());
        }
        List<FlashPromotionSession> flashPromotionSessionList=map.map(item->
            FlashPromotionSessionVo flashPromotionSessionVo=new FlashPromotionSessionVo();
            FlashPromotionSession byId = flashPromotionSessionService.getById(item.getFlashPromotionSessionId());
            BeanUtils.copyProperties(byId,flashPromotionSessionVo);
            flashPromotionSessionVo.setStartTime(byId.getStartTime());
            flashPromotionSessionVo.setEndTime(byId.getEndTime());
            return flashPromotionSessionVo;
        }).collect(Collectors.toList());
        List<FlashPromotionSession> flashPromotionSessionList=list.stream().map(
                item->{
                    Long flashPromotionSessionId = item.getFlashPromotionSessionId();
                    FlashPromotionSession byId = flashPromotionSessionService.getById(flashPromotionSessionId);
                    return byId;
                }
        ).collect(Collectors.toList());*/

    }


}
